package StudyCode.summary.textfile;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.*;
import java.util.Set;

/**
 * 版权所有 科技与人文(www.tah1986.com)
 */
public class POSIXPermissionsFileToSameFile {
	public static void main(String[] args) {

		PosixFileAttributes attr = null;
		Path path = Paths.get("E:/Copy/test1.txt");
		try {
			attr = Files.getFileAttributeView(path, PosixFileAttributeView.class)
					.readAttributes();
		} catch (IOException e) {
			System.err.println(e);
		}
		System.out.println("文件所有者: " + attr.owner().getName());
		System.out.println("文件分组: " + attr.group().getName());
		System.out.println("文件权限: " + attr.permissions().toString());

		Path new_path = Paths.get("/home/folder1/folder2/folder3/new_test.txt");
		FileAttribute<Set<PosixFilePermission>> posixattrs =
				PosixFilePermissions.asFileAttribute(attr.permissions());
		try {
			Files.createFile(new_path, posixattrs);
		} catch (IOException e) {
			System.err.println(e);
		}
	}
}
